<template>
  <div id="charge" v-loading="loading">
    <v-tableTitle
      :btnConfig="btnConfig"
      :tableSelectData="tableSelectData"
      :title="$t('bill.chargeList')"
      @del="del"
      @newCreate="newCreate"
      @exportFile="exportFile"
      @upFile="upFile"
      @freeze="freeze"
      @noResult="noResult"
      @recount="recount"
    ></v-tableTitle>
    <v-table
      :page="page"
      :pageSize="pageSize"
      :tableConfig="tableConfig"
      :tableData="tableData"
      :total="total"
      @findData="getTableData"
      @getTableData="getTableData"
      @searhData="searhData"
      @selectData="selectData"
      @setdialog="setdialog"
      @upData="getTableData"
      ref="table"
    ></v-table>
  </div>
</template>

<script type="text/ecmascript-6">
import tableTitle from "commons/TableTitle";
import table from "commons/Table";
import baseMixin from "@/mixins/BaseMixin";
import config from "./Config";

export default {
    name: "charge",
    mixins: [baseMixin, config],
    data() {
        return {
            loading: false,
            tableConfig: [
                {
                    label: this.$t('bill.customer'),
                    prop: "cust_code",
                    input: "input",
                },
                {
                    label: this.$t('bill.billTo'),
                    prop: "bill_to",
                    input: "input",
                },
                {
                    label: this.$t('bill.wareHouse'),
                    prop: "facility_id",
                    input: "select",
                    options: this.getOptions("getSelectCode/FACILITY_ID/NAME/BIC_FACILITY/1=1")
                },
                {
                    label: this.$t('bill.activityId'),
                    prop: "activity_id",
                    input: "select",
                    options: this.getOptions("getSelectCode/ACTIVITY_ID/ACTIVITY_DESC/BIC_ACTIVITY/1=1")
                },
                {
                    label: this.$t('bill.orderNo'),
                    prop: "order_no",
                    input: "input",
                },
                {
                    label: this.$t('bill.orderType'),
                    prop: "order_type",
                    input: "input",
                },
                {
                    label: this.$t('bill.orderLine'),
                    prop: "order_line",
                    input: "input",
                },
                {
                    label: this.$t('bill.chargeDate'),
                    prop: "charge_date",
                    input: "date",
                },
                {
                    label: this.$t('bill.chargeCode'),
                    prop: "charge_code",
                    input: "input",
                },
                {
                    label: this.$t('bill.chargeQty'),
                    prop: "charge_qty",
                    input: "input",
                },
                {
                    label: this.$t('bill.chargeUom'),
                    prop: "charge_uom",
                    input: "select",
                    options: this.getOptions("getSelectCode/UOM_CODE/UOM_DESC/BIC_UOM/1=1")
                },
                {
                    label: this.$t('bill.chargeRate'),
                    prop: "charge_rate",
                    input: "input",
                },
                {
                    label: this.$t('bill.chargeAmount'),
                    prop: "charge_amt",
                    input: "input",
                },
                {
                    label: this.$t('bill.invoiceStatus'),
                    prop: "invoice_status",
                    input: "select",
                    options: this.getOptions("getSelectCode/INVOICE_STATUS/INV_STATUS_DESC/BIC_CODE_INVSTATUS/language='zh'")
                },
                {
                    label: this.$t('bill.batchNo'),
                    prop: "batch_no",
                    input: "input",
                },
                {
                    label: this.$t('bill.invoice'),
                    prop: "invoice_no",
                    input: "input",
                },
                {
                    label: this.$t('bill.chargeNot'),
                    prop: "charge_no",
                    input: "input",
                },
            ],
            tableData: [],
            pageSize: 10,
          page: 1,
            total: 0,
            tableSelectData: [],
            tableSearhData: {},
            wms: ''
        };
    },
    computed: {
        btnConfig() {
            return [
                {label: this.$t('common.create'), certigier: this.newStatus, invalid: "newCreate"},
                {label: this.$t('common.delete'), certigier: this.deleteStatus, disabled: true, invalid: "del"},
                {label: this.$t('bill.freeze'), certigier: this.deleteStatus, disabled: true, invalid: "freeze"},
                {label: this.$t('bill.noResult'), certigier: this.deleteStatus, disabled: true, invalid: "noResult"},
                {label: this.$t('bill.recount'), certigier: this.deleteStatus, disabled: true, invalid: "recount"},
                {label: this.$t('common.export'), certigier: this.deleteStatus, disabled: true, invalid: "exportFile"},
                {type: "file1", label: this.$t('common.import'), certigier: this.uploadStatus, invalid: "file"},
            ]
        }
    },
    created() {
    },
    methods: {
        async freeze(data) {
            try {
                let res = await this.$api.post(`/holdCharge/${this.$store.state.wms}`, data);
                
                    this.$message.success(res.returnMgr);
                    let page = sessionStorage.getItem('page');
                    this.getTableData(page, this.tableSearhData);
                    
                
            } catch (e) {
                this.$message.error(e);
            }
        },
        async noResult(data) {
            try {
                let res = await this.$api.post(`/noHoldCharge/${this.$store.state.wms}`, data);
                
                    this.$message.success(res.returnMgr);
                    let page = sessionStorage.getItem('page');
                    this.getTableData(page, this.tableSearhData);
                    
                
            } catch (e) {
                this.$message.error(e);
            }
        },
        async upFile(data) {
            try {
                let res = await this.$api.post('/uploadCharge', data);
                    this.$message.success(res.returnMgr);
                    this.getTableData(1, this.tableSearhData);
            } catch (e) {
                this.$message.error(e);
            }
        },
        async recount(data) {
            try {
                let res = await this.$api.post(`/recalculateCharge/${this.$store.state.wms}`, data);
                
                    this.$message.success(res.returnMgr);
                    let page = sessionStorage.getItem('page');
                    this.getTableData(page, this.tableSearhData);
                    
                
            } catch (e) {
                this.$message.error(e);
            }
        },
        async exportFile(data) {
            try {
                let res = await this.$api.post('/downCharge', data, {responseType: 'blob'})
                if (window.navigator.msSaveBlob) {
                    var blobObject = new Blob([res.data]);
                    window.navigator.msSaveBlob(blobObject, 'charge.xls');
                } else {
                    var blob = new Blob([res.data], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'});
                    var filename = 'charge.xls';
                    var downloadElement = document.createElement('a');
                    var href = window.URL.createObjectURL(blob); //创建下载的链接
                    downloadElement.style.display = 'none';
                    downloadElement.href = href;
                    downloadElement.download = filename; //下载后文件名
                    document.body.appendChild(downloadElement);
                    downloadElement.click(); //点击下载
                    document.body.removeChild(downloadElement); //下载完成移除元素
                    window.URL.revokeObjectURL(href); //释放掉blob对象
                }
            } catch (e) {
                this.$message.error(e);
            } finally {
                
            }
        },
        //新建
        async newCreate() {
            this.$store.commit("setNewFlag", true);
            this.$router.push({name: 'chargeDeawer'});
        },
        //选中的内容
        selectData(data) {
            this.tableSelectData = data;
        },
        //删除
        async del(data) {
            try {
                let res = await this.$api.post(`/deleteCharge/${this.$store.state.wms}`, data);
                
                    this.$message.success(res.returnMgr);
                    let page = sessionStorage.getItem('page');
                    this.getTableData(page, this.tableSearhData);
                    
                
            } catch (e) {
                this.$message.error(e);
            }
        },
        async getTableData(page, data) {
            try {
                this.loading = true;
                let res = await this.$api.post(`/queryChargeList/${this.$store.state.wms}/${page}`, data);
                
                    this.tableData = res.page.list;
                  this.page = res.page.curPage;
                    this.pageSize = res.page.pageSize;
                    this.total = res.page.total;
                
            } catch (e) {
                this.$message.error(e);
            } finally {
                this.loading = false;
            }
        },
        //搜索的值
        searhData(data, up) {
            if (!up) {
                this.tableSearhData = data;
            } else {
                this.tableSearhData = data;
                this.getTableData(1, this.tableSearhData);
            }
        },
        //设置弹框
        setdialog(data) {
            this.$store.commit("setRouteData", data);
            this.$router.push({name: 'chargeDeawer'});
        },
    },
    mounted() {
        
        this.wms = this.$store.state.wms;
        this.$store.commit("setSkip", {});
        this.$store.commit("setNewFlag", false);
        this.$store.commit("setRouteData", {});
        this.getTableData(1, {});
    },
    watch: {
        tableSelectData(data) {
            if (this.permissionStatus) {
                if (data.length > 0) {
                    this.deleteStatus = false;
                } else {
                    this.deleteStatus = true;
                }
            }
        }
    },
    components: {
        "v-tableTitle": tableTitle,
        "v-table": table,
    }
};
</script>

<style scoped>
#charge {
  width: 100%;
  height: 99%;
  padding-top: 6px;
}
</style>
